import request from '../../utils/api/request'
// 获取应用实例
const app = getApp() 

Page({
    data:{
      voteList:null,
      userInfo:null,
      code:null,
      user_id:null
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    console.log('onload')
     let userInfo = wx.getStorageSync('userInfo') || {}
     if( userInfo && (JSON.stringify(userInfo) != "{}")){
       let user_id = wx.getStorageSync('userId')
       this.setData({userInfo,user_id})
      //  this.setData({
      //   user_id
      //  })
       this.getUserVoteALl(user_id)
     }

  },
  async getUserVoteALl(user_id){
    const result = await request(
                          '/api/front/vote/getAllvote',
                          {user_id}
                         )
    if(result.err_code == 200 ){
      this.setData({
        voteList:result.data.reverse()
      })
      return
    }
    //获取记录失败
    wx.showToast({
      title: result.message,
      icon: 'error',
      duration: 2000
    })
  },


  getUserProfile1(e) {
    wx.login({
      success: (res) =>{
        if (res.code) {
          console.log(res.code)
          this.setData({
            code:res.code
          })
        }
      }
    })


    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: async (res) => {
        let result = await request(
                            '/api/front/user/basic/wx_login',
                            {code:this.data.code,name:res.userInfo.nickName,img:res.userInfo.avatarUrl}
                          )
        if ( result.err_code == 200){
          this.setData({
            userInfo: res.userInfo,
            user_id: result.data.user_id
          })
          wx.setStorageSync('userInfo',res.userInfo)
          wx.setStorageSync('openId', result.data.openid)
          wx.setStorageSync('userId', result.data.user_id)
          //获取用户所有投票记录
          this.getUserVoteALl(result.data.user_id)
          
          
        }else{
          wx.showToast({
            title: '网络错误',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })


  
  },



  //当前被点击的节点
  currentItem(ev){
      //获取当前点击的一行
      let currentIndex = ev.currentTarget.dataset.index
      //falg为真，能在当前行下面显示四个icon
      let voteList = this.data.voteList.map((item,index)=>{
        if ( currentIndex == index ){
          item.falg = !item.falg
          return item
        }else{
          item.falg = false
        }
        return item
      })
      this.setData({voteList})
  },
  
  edit(ev){
    let vote_id = ev.currentTarget.dataset.id
    console.log(vote_id,'唯一标识')
    wx.navigateTo({
      url:`/pages/vote/vote?id=${vote_id}`
    })
  },
  look(ev){
    let vote_id = ev.currentTarget.dataset.id
    wx.navigateTo({
      url:`/pages/votoDetail/votoDetail?id=${vote_id}`
    })
  },
  share(){
    console.log('分享')
  },
  delte(ev){
    wx.showModal({
      title: '提示',
      content: '确认删除?',
      success:async(res)=> {
        // let user_id = wx.getStorageInfoSync('userId')
        let vote_id = ev.currentTarget.dataset.id
        let user_id = this.data.user_id
        //确认删除
        if (res.confirm) {
          const result = await request(
                          '/api/front/vote/delete_vote',
                          {vote_id,user_id}
                        )
          if ( result.err_code == 200){
            wx.showToast({
              title: '删除成功',
              icon:'none'
            })
            this.getUserVoteALl(this.data.user_id)
            return 
          }
          wx.showToast({
            title: result.message,
            icon:'none'
          })



        }
      }
    })
  },
  /**
   * 跳转到推荐页面
   */
  handleToRecommendSong (){
    // wx.navigateTo({
    //   url:'/songPage/pages/recommendSong/recommendSong'
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // console.log('onReady')
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let userInfo = wx.getStorageSync('userInfo') || {}
    if( userInfo && (JSON.stringify(userInfo) != "{}")){
      this.setData({userInfo})
      this.getUserVoteALl(wx.getStorageSync('userId'))
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function ( res ) {
    let voteId = res.target.dataset.id
    console.log(voteId)
    let nickName = this.data.userInfo.nickName
    if ( res.from === 'button'){
      console.log('but')
      return {
        title:`${nickName}给您发来了一个投票`,
        imageUrl:'/static/images/toupiao.png',
        path:`/pages/votoDetail/votoDetail?id=${voteId}`
      }
    }
    return{
      title:"全局转发"
    }
  }
})
